package com.dognessnetwork.customer;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;

import javax.websocket.ClientEndpoint;
import javax.websocket.ContainerProvider;
import javax.websocket.DeploymentException;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.dognessnetwork.customer.dto.Msg;

@ClientEndpoint()
@Component
public class CustomerWebSocketClient{
	
	//@Value("${customer.wesocket}")
    private String wesocket="ws://192.168.0.139:8088/websocket";
	
    private static Session session;
    
    public void send(Msg msg) {
        WebSocketContainer container = null;
        try {
            container = ContainerProvider.getWebSocketContainer();
        } catch (Exception ex) {
            System.out.println("error" + ex);
        }
        try {
            URI r = URI.create(wesocket+"/"+msg.getToUser());
            session = container.connectToServer(CustomerWebSocketClient.class, r);
        } catch (DeploymentException | IOException e) {
            e.printStackTrace();
        }
        
        session.getAsyncRemote().sendText(msg.toString());
    }
}
